package com.buddies.lottery.ui.user.pwd;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.buddies.lottery.App;
import com.buddies.lottery.R;
import com.buddies.lottery.base.BaseActivity;
import com.buddies.lottery.data.bean.User;
import com.buddies.lottery.ui.MainActivity;
import com.r0adkll.slidr.Slidr;

public class ResetPasswordActivity extends BaseActivity<ResetPasswordContract.Presenter> implements View.OnClickListener, ResetPasswordContract.View {

    private static final int REQUEST_CODE_REGISTER = 1;

    private Button mLoginBTN;
    private EditText mPhoneET;
    private EditText mPwdET;
    private ImageView mClearPhoneIV;
    private ImageView mClearPwdIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        Slidr.attach(this, App.getSlidrConfig());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    @Override
    protected void initView() {
        mLoginBTN = (Button) findViewById(R.id.btn_login);
        mPhoneET = (EditText) findViewById(R.id.et_phone);
        mPwdET = (EditText) findViewById(R.id.et_pwd);
        mClearPhoneIV = (ImageView) findViewById(R.id.iv_clear);
        mClearPwdIV = (ImageView) findViewById(R.id.iv_clear_pwd);

        mLoginBTN.setOnClickListener(this);
        mClearPhoneIV.setOnClickListener(this);
        mClearPwdIV.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        setUp();
        setTitle(getResources().getString(R.string.reset_password));

        mPresenter = new ResetPasswordPresenter(this);

        mPhoneET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mClearPhoneIV.setVisibility(editable.length() == 0 ? View.GONE : View.VISIBLE);
                setCommitBtnEnable();
            }
        });
        mPhoneET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                mClearPhoneIV.setVisibility(mPhoneET.length() == 0 || !hasFocus ? View.GONE : View.VISIBLE);
            }
        });

        mPwdET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mClearPwdIV.setVisibility(editable.length() == 0 ? View.GONE : View.VISIBLE);
                setCommitBtnEnable();
            }
        });
        mPwdET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                mClearPwdIV.setVisibility(mPwdET.length() == 0 || !hasFocus ? View.GONE : View.VISIBLE);
            }
        });

        setCommitBtnEnable();
    }

    public void setCommitBtnEnable() {
        boolean enabled = mPhoneET.length() > 0 && mPwdET.length() > 0;
        mLoginBTN.setEnabled(enabled);
        if (!enabled) {
            mLoginBTN.setAlpha(0.3f); //TextColor(getResources().getColor(R.color.black_30_percent_transparent));
        } else {
            mLoginBTN.setAlpha(1.0f); //TextColor(getResources().getColor(R.color.black));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if (checkPhone() && checkPwd()) {
                    requestResetPassword();
                }
                KeyboardUtils.hideSoftInput(getActivity());
                break;

            case R.id.iv_clear:
                mPhoneET.setText("");
                break;

            case R.id.iv_clear_pwd:
                mPwdET.setText("");
                break;

            default:
                break;
        }
    }

    private void requestResetPassword() {
        final String phone = getPhone();
        final String pwd = getPwd();

        mPresenter.postResetPwd(EncryptUtils.encryptMD5ToString(phone), EncryptUtils.encryptMD5ToString(pwd));
        showProgressDialog(R.string.register_commit_processing);
    }

    @Override
    public void onResetPwdSuccess(User user) {
        ToastUtils.showLong(R.string.reset_password_success);

        if (isTaskRoot()) {
            MainActivity.launch(getActivity());
        }

        setResult(RESULT_OK);
        finish();
    }

    private boolean checkPhone() {
        String phone = getPhone();

        boolean check = false;
        if (TextUtils.isEmpty(phone)) {
            showToast(getResources().getString(R.string.login_password_require));
        } else if (phone.length() < 6 || phone.length() > 20) {
            showToast(getResources().getString(R.string.login_password_condition));
        } else {
            check = true;
        }

        return check;
    }

    public String getPhone() {
        return mPhoneET.getText().toString();
    }

    private boolean checkPwd() {
        String pwd = getPwd();

        boolean check = false;
        if (TextUtils.isEmpty(pwd)) {
            showToast(getResources().getString(R.string.login_password_require));
        } else if (pwd.length() < 6 || pwd.length() > 20) {
            showToast(getResources().getString(R.string.login_password_condition));
        } else {
            check = true;
        }

        return check;
    }

    public String getPwd() {
        return mPwdET.getText().toString();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    public static void launch(Context context) {
        Intent intent = new Intent(context, ResetPasswordActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

}
